//-----------------------------------------------------------------------
// <copyright file="DeleteVirtualDirectory.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <summary>Deletes the specfied Virtual Directory.</summary>
//-----------------------------------------------------------------------
namespace Microsoft.Sdc.Tasks.Web.WebSite
{
    using System;
    using Microsoft.Sdc.Tasks.Configuration.Web;
    using Microsoft.Build.Framework;

    /// <summary>
    /// Deletes the specified virtual directory from the specified server and web site
    /// </summary>
    /// <remarks>
    /// <code><![CDATA[<Web.WebSite.DeleteVirtualDirectory
    ///     VirtualDirectoryName="virtualDirectoryName"
    ///     MachineName="machineName"
    ///     WebSiteName="webSiteName" /> ]]></code>
    /// <para>where:</para>
    /// <para><i>virtualDirectoryName (required)</i></para>
    /// <para>The virtual directory to delete</para>
    /// <para><i>machineName</i></para>
    /// <para>Machine to perform delete on</para>
    /// <para><i>webSiteName</i></para>
    /// <para>Web site to delete the virtual directory from</para>
    /// </remarks>
    /// <example>
    /// <code><![CDATA[
    /// <Project>
    ///     <Target Name="Test" >
    ///         <Web.WebSite.DeleteVirtualDirectory
    ///             VirtualDirectoryName="/test"
    ///             Description="testwebsite" />
    ///     </Target>
    /// </Project>
    /// ]]></code>    
    /// </example>    
    public class DeleteVirtualDirectory : TaskBase
    {
        private string name = String.Empty;
        private string machineName = "localhost";
        private string webSiteName = "Default Web Site";

        /// <summary>
        /// Virtual directory to delete
        /// </summary>
        /// <value>A valid, existing virtual directory</value>
        [Required]
        public string VirtualDirectoryName
        {
            get { return (this.name ?? String.Empty); }
            set { this.name = value; }
        }

        /// <summary>
        /// Machine to perform the delete action on
        /// </summary>
        /// <value>A valid machine name. If this value is not set the delete will 
        /// target on the local machine.</value>
        public string MachineName
        {
            get { return (this.machineName ?? String.Empty); }
            set { this.machineName = value; }
        }

        /// <summary>
        /// Web site to delete the virtual directory from
        /// </summary>
        /// <value>A value web site name. If this value is not set the delete will
        /// target against "Default Web Site"</value>
        public string WebSiteName
        {
            get { return (this.webSiteName ?? String.Empty); }
            set { this.webSiteName = value; }
        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            WebSite web = WebSite.Load(this.machineName, this.webSiteName);
            VirtualDirectory vdir = web.VirtualDirectories[this.name];
            if (vdir != null)
            {
                Log.LogMessageFromResources("Web.DeleteVirtualDirectory", this.name);
                vdir.AppDelete();
                vdir.Delete();
            }
        }
    }
}